package com.wanxi.coupons.service.util;

import com.wanxi.coupon.api.vo.CouponAdminTDO;
import com.wanxi.coupon.api.vo.CouponEnterpriseTDO;
import com.wanxi.coupons.exception.ExceptionResult;
import com.wanxi.coupons.exception.MyException;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ClassName: Verify
 * Description: 自定义校验
 * date: 2022/1/14 16:27
 *
 * @author lxl
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class Verify {

    private static Logger log ;
    public boolean admin(CouponAdminTDO couponAdminTDO) throws ParseException {
        if (couponAdminTDO.getAdminId()==null || couponAdminTDO.getAdminId()<0) {
            log.info("管理员id不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"管理员id不能为空");
        }
        if (couponAdminTDO.getCouponName()==null){
            log.info("优惠券名称不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券名称不能为空");
        }
        if (couponAdminTDO.getEndTime()==null){
            log.info("优惠券过期时间不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能为空");
        }
        if (couponAdminTDO.getQuota()==null || couponAdminTDO.getQuota()<1){
            log.info("优惠券数量不能为空且小于1");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券数量不能为空且小于1");
        }
        if (couponAdminTDO.getStartTime()==null){
            log.info("优惠券名称开始时间不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券名称开始时间不能为空");
        }
        if (couponAdminTDO.getType()==null ||couponAdminTDO.getType()>10 ||couponAdminTDO.getType()<0){
            log.info("优惠券名类型错误");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券名类型错误");
        } if (couponAdminTDO.getEnterpriseRange().isEmpty()){
            log.info("优惠券适用范围不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券适用范围不能为空");
        }

        if (couponAdminTDO.getEndTime()!=null&&couponAdminTDO.getStartTime()!=null){
            if ("2099-12-31".equals(couponAdminTDO.getEndTime())){
                throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能太长");
            }
            String start = couponAdminTDO.getStartTime().replace("-", "");
            String eng = couponAdminTDO.getEndTime().replace("-", "");
            Long i1 = (Long.valueOf(eng) - Long.valueOf(start))/10000;

            if (i1<0){
                log.info("优惠券过期时间不能小于生效时间");
                throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能小于生效时间");
            }
            long i = calculateTime(couponAdminTDO.getEndTime(), couponAdminTDO.getStartTime());
            if (i==0){
                log.info("优惠券过期时间不能小于一天");
                throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能小于一天");
            }

            couponAdminTDO.setPastTime(i);
            return true;
        }

        return true;
    }

    public boolean enterprise(CouponEnterpriseTDO couponEnterpriseTDO) throws ParseException {

        if (couponEnterpriseTDO.getEnterpriseId()==null||couponEnterpriseTDO.getEnterpriseId()<0){
            log.info("商家id不能为空且为整数");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"商家id不能为空且为整数");
        }
        if (couponEnterpriseTDO.getCouponName()==null){
            log.info("优惠券名称不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券名称不能为空");
        }
        if (couponEnterpriseTDO.getSkuRange().isEmpty()){
            log.info("优惠券适用范围不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券适用范围不能为空");
        }
        if (couponEnterpriseTDO.getEndTime()==null){
            log.info("优惠券过期时间不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能为空");
        }
        if (couponEnterpriseTDO.getQuota()==null || couponEnterpriseTDO.getQuota()<1){
            log.info("优惠券数量不能为空且大于1");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券数量不能为空且大于1");
        }
        if (couponEnterpriseTDO.getStartTime()==null){
            log.info("优惠券名称开始时间不能为空");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券名称开始时间不能为空");
        }
        if (couponEnterpriseTDO.getType()==null ||couponEnterpriseTDO.getType()>10 ||couponEnterpriseTDO.getType()<0){
            log.info("优惠券类型错误");
            throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券类型错误");
        }
        if (couponEnterpriseTDO.getEndTime()!=null&&couponEnterpriseTDO.getStartTime()!=null){

            String start = couponEnterpriseTDO.getStartTime().replace("-", "");
            String eng = couponEnterpriseTDO.getEndTime().replace("-", "");
            Long i1 = (Long.valueOf(eng) - Long.valueOf(start))/10000;

            if (i1<0){
                log.info("优惠券过期时间不能小于生效时间");
                throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能小于生效时间");
            }
            long i = calculateTime(couponEnterpriseTDO.getEndTime(), couponEnterpriseTDO.getStartTime());
            if (i==0){
                log.info("优惠券过期时间不能小于一天");
                throw new MyException(ExceptionResult.EXCEPTION_NULL.getCode(),"优惠券过期时间不能小于一天");
            }

            couponEnterpriseTDO.setPastTime(i);
            return true;
        }

        return true;
    }

// 统计字符串出现的次数
    private int getString(String str){

        if (str.length()<1){
            return 0;
        }
        char[] chars = str.toCharArray();
        int num = 0;
        for (char aChar : chars) {
            if ('-' == aChar) {
                num++;
            }
        }
        return num;
    }

//    计算时间返回天数
    public long calculateTime(String time1,String time2) throws ParseException {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date star = formatter.parse(time1);//开始时间
        Date end=formatter.parse(time2);//结束时间
        Long starTime=star.getTime();
        Long endTime=end.getTime();
        Long num=Math.abs(endTime-starTime);//时间戳相差的毫秒数
        long l = (num / 24 / 60 / 60 / 1000)*24*60;
        System.out.println("相差天数为："+num / 24 / 60 / 60 / 1000+" ,相差分钟为："+l);
            return l;
    }
}
